<template>
  <el-container>
    <el-main class="el-main">
      <div class="main_box">
        <el-header>报告管理</el-header>
        <div class="btn_rows">
          <el-button @click="insertLabel">插入标签</el-button>
          <el-button @click="insertReport">插入图表</el-button>
          <el-button @click="preview">预览</el-button>
        </div>
        <!-- <div v-html="formHtml"></div> -->
        <report-editor class="right_editor" ref="reportEditor" v-model="formHtml"></report-editor>
      </div>
    </el-main>
    <vue-context-menu
      :contextMenuData="contextMenuData"
      @left="left"
      @center="center"
      @right="right"
      style="background: #142957; box-shadow: 0 2px 2px 0 #0a43bc; border: 1px solid #00c5ff"
    ></vue-context-menu>
  </el-container>
</template>

<script>
/* 
报告编辑生成
*/
import reportEditor from "./reportEditor.vue";
import * as echarts from "echarts";
export default {
  name: "",
  props: {},
  components: { reportEditor },
  data() {
    return {
      formHtml: "",
      reportId: 1,
      contextMenuTarget: null,
      contextMenuVisible: false,
      eventId: null,
      // 菜单数据
      contextMenuData: {
        menuName: "demo",
        // 菜单显示的位置
        axis: {
          x: null,
          y: null,
        },
        // 菜单选项
        menulists: [
          {
            fnHandler: "left", // 绑定事件
            icoName: "icon iconfont icon-bianji2", // icon图标
            className: "rightMentli",
            btnName: "左对齐", // 菜单名称
          },
          {
            fnHandler: "center", // 绑定事件
            icoName: "icon iconfont icon-bianji2", // icon图标
            className: "rightMentli",
            btnName: "居中", // 菜单名称
          },
          {
            fnHandler: "right", // 绑定事件
            icoName: "icon iconfont icon-bianji2", // icon图标
            className: "rightMentli",
            btnName: "右对齐", // 菜单名称
          },
        ],
      },
      option: {
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: "line",
          },
        ],
      },
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.setRightMenu();
  },
  methods: {
    preview() {
        this.$store.commit('SET_HTML',this.formHtml)
        this.$router.push('/preview')
    },
    showMenu(e) {
      e.preventDefault();
      var x = e.clientX;
      var y = e.clientY;
      // Get the current location
      this.contextMenuData.axis = {
        x,
        y,
      };
    },
    left() {
      let dom = document.querySelector("#" + this.eventId);
      dom.style.justifyContent = "flex-start";
    },
    center() {
      let dom = document.querySelector("#" + this.eventId);
      dom.style.justifyContent = "center";
    },
    right() {
      let dom = document.querySelector("#" + this.eventId);
      dom.style.justifyContent = "flex-end";
    },
    setRightMenu() {
      this.$nextTick(() => {
        // vue-context-menu 需要传入一个触发右键事件的元素，等页面 dom 渲染完毕后才可获取
        // 获取所有的 treeitem，循环监听右键事件
        const tree = document.querySelectorAll(".report");
        tree.forEach((i) => {
          i.addEventListener("contextmenu", (event) => {
            // 如果右键了，则模拟点击这个treeitem
            this.eventId = i.getAttribute("id");
            this.showMenu(event);
            // event.target.click();
          });
        });
      });
    },
    insertTarget(selectedTarget) {
      this.$refs.reportEditor.insertTarget(selectedTarget);
    },
    insertLabel() {
      this.insertTarget({
        targetName: "测试标签",
        id: 123,
      });
    },
    insertReport() {
      this.$refs.reportEditor.setimg({
        // url: "/img/test.jpg",
        id: "aaa" + this.reportId,
        style: "display:flex;justify-content:center",
        style2: "width:300px;height:240px;",
      });
      this.setChart();
    },
    setChart() {
      this.$nextTick(() => {
        let dom = document.querySelector("#aaa" + this.reportId);
        this.reportId += 1;
        let _myChart = echarts.init(dom);
        _myChart.setOption(this.option);
        this.setRightMenu();
        console.log(this.formHtml);
      });
    },
  },
  watch: {},
  filters: {},
};
</script>

<style scoped lang='scss'>
.el-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #c8c8c8;
}
.el-header {
  height: 40px;
  line-height: 40px;
}
.el-main {
  flex: 1;
  height: 0;
}
.main_box {
  width: 800px;
  margin: 0 auto;
  background-color: #fff;
  padding: 20px;
}
::v-deep .right_editor {
  .ql-editor {
    height: 400px !important;
  }
}
.btn_rows {
  margin-bottom: 20px;
}
img {
  user-select: none;
}
.right-menu {
  font-size: 14px;
  position: fixed;
  background: #fff;
  border: solid 1px rgba(0, 0, 0, 0.2);
  border-radius: 3px;
  z-index: 999;
  display: none;
}
.right-menu a {
  width: 150px;
  height: 28px;
  line-height: 28px;
  text-align: center;
  display: block;
  color: #1a1a1a;
}
</style>